//
//  BoundDeviceCell.swift
//  EasePods
//
//  Created by 李然 on 2024/4/8.
//

import UIKit
import Lottie
import EarbudsFunctions

class BoundDeviceCell: TableViewCell {
    
    override func makeUI() {
        super.makeUI()
        
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.addSubview(containView)
        containView.snp.makeConstraints { make in
            make.edges.equalTo(UIEdgeInsets(top: 10, left: 20, bottom: 10, right: 20))
            make.height.greaterThanOrEqualTo(111)
        }
        
        containView.addSubview(deviceImageView)
        deviceImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview()
            make.width.equalTo(100)
        }
        
        containView.addSubview(arrowView)
        arrowView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalTo(-20)
        }
        
        let stackView = StackView()
        stackView.axis = .vertical
        stackView.distribution = .fill
        stackView.alignment = .leading
        stackView.spacing = 8
        stackView.addArrangedSubview(deviceNameLabel)
        stackView.addArrangedSubview(macAddressLabel)
        stackView.addArrangedSubview(chargingView)
        
        containView.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(deviceImageView.snp.trailing)
        }
    }
    
    func bind(viewModel: BoundDeviceViewModel) {
        deviceNameLabel.text = viewModel.device.localName
        macAddressLabel.text = viewModel.device.discovery?.mac
        chargingView.isHidden = !viewModel.device.isCharging
    }
    
    lazy var containView: View = {
        let view = View()
        view.backgroundColor = R.color.common_9_bg()
        view.layer.cornerRadius = 6
        return view
    }()
    
    lazy var deviceImageView: UIImageView = {
        let view = UIImageView()
        view.image = R.image.img_common_earcase()
        view.contentMode = .scaleAspectFit
        return view
    }()
    
    lazy var deviceNameLabel: UILabel = {
        let label = UILabel()
        label.textColor = R.color.common_3_text()
        label.font = Fonts.systemFont(ofSize: 18)
        label.numberOfLines = 0
        return label
    }()
    
    lazy var chargingView: UIImageView = {
        let view = UIImageView()
        view.image = R.image.ic_home_battery_ing()
        view.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 26, height: 14))
        }
        return view
    }()
    
    lazy var macAddressLabel: UILabel = {
        let label = UILabel()
        label.textColor = R.color.common_23_text_second()
        label.font = Fonts.systemFont(ofSize: 12)
        label.numberOfLines = 0
        return label
    }()
   
    lazy var arrowView: ImageView = {
        let view = ImageView()
        view.image = R.image.ic_common_more_large()
        return view
    }()
}
